// pages/addressList/addressList.js
Page({

  /**
   * 页面的初始数据
   */

  data: {
    pickerHidden: true,
    chosen: '',
    showadd: false,
    showedit:false,
    addresslist:[
      {
        name:'赵江龙',
        phone:'18715680170',
        address:'广州从化粤嵌科技',
        checked:false,
      },
      {
        name:'小赵',
        phone:'18888888',
        address:'广州从化粤嵌科技',
        checked:false
      },
    ],
    name:'',
    phone:'',
    address:'',
    listIndex:null,
  },

  //新增
  showaddPopup() {
    this.setData({ showadd: true });
  },
  //修改
  showeditPopup(e) {
    let index = e.currentTarget.dataset.index
    let {name,phone,address} = this.data.addresslist[index]
    this.setData({ 
      showedit: true,
      name:name,
      phone:phone,
      address:address,
      listIndex:index
     });
  },
  //修改
  edit(e){
    console.log(e);
    let {name,phone,address} = e.detail.value
    this.data.addresslist[this.data.listIndex].name=name
    this.data.addresslist[this.data.listIndex].phone=phone
    this.data.addresslist[this.data.listIndex].address=address
    wx.setStorageSync('addresslist',this.data.addresslist)
    this.data.showedit=false,
    this.setData(this.data)
  },
  //添加
  add(e){
    let {name,phone,address} = e.detail.value
    this.data.addresslist=[...this.data.addresslist,{name,phone,address,checked:false}]
    wx.setStorageSync('addresslist',this.data.addresslist)
    this.data.showadd=false
    name=''
    phone=''
    address=''
    this.setData(this.data)
  },
  onaddClose() {
    this.setData({ showadd: false });
  },
  oneditClose() {
    this.setData({ showedit: false });
  },
  //设为默认
  checked(e){
    let index = e.currentTarget.dataset.index
    this.data.addresslist.forEach(item=>item.checked=false)
    this.data.addresslist[index].checked=true
    wx.setStorageSync('addresslist',this.data.addresslist)
    this.setData(this.data)
  },
  //删除地址
  del(e){
    let index = e.currentTarget.dataset.index
    if (this.data.addresslist[index].checked) {
      wx.removeStorageSync('addressIndex')
    }
    this.data.addresslist.splice(index,1)
    wx.setStorageSync('addresslist',this.data.addresslist)
    this.setData(this.data)
  },
  //选择地址
  selectad(e){
    let pages = getCurrentPages(); // 页面对象
    let prevpage = pages[pages.length - 2]; // 上一个页面对象
    let path = prevpage.route;
    let index = e.currentTarget.dataset.index
    if (path=='pages/pay/pay') {
      wx.setStorageSync('currentAddress',this.data.addresslist[index])
      wx.navigateTo({
      url: '/pages/pay/pay',
    })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let temparr = wx.getStorageSync('addresslist')
    this.data.addresslist=temparr==null?this.data.addresslist:temparr
    this.setData(this.data)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})